
// This JavaScript was written by Tyler Akins and is licensed under
// the GPL v3 -- http://www.gnu.org/copyleft/gpl.html
// See it on the original site -- http://rumkin.com/tools/gps/degrees.php
// Feel free to copy it to your site as long as you leave this header
// pretty much intact and as long as you are complying with the GPL.

function GetDegreeValue(v)
{
    var vv = "";
    var good = "0123456789.";
    var sign = 1;
    var factor = 1;
    var d = 0;
    var c, oldc;
    
    // Change non-numbers into spaces.
    oldc = ' ';
    for (i = 0; i < v.length; i ++)
    {
        var c = v.charAt(i).toUpperCase();
	if (c == 'W' || c == 'S' || c == '-')
	{
	    sign = -1;
	}
	if (good.indexOf(c) < 0)
	{
	    c = ' ';
	}
	if (oldc != ' ' || c != ' ')
	{
	    vv += c;
	    oldc = c;
        }	
    }

    v = new Array();
    v = vv.split(' ');
    
    for (i = 0; i < v.length; i ++)
    {
	d += v[i] * factor;
	factor /= 60;
    }
    
    return d * sign;
}


function DoPrecision(v, p)
{
    return Math.round(v * Math.pow(10, p)) / Math.pow(10, p);
}


function upd(v,out)
{
    var d, m, sign = '', str;
    
    v = GetDegreeValue(v);
    if (v < 0)
    {
        sign = '-';
	v = - v;
    }
    
    str = '<span class="gCord">'+sign + DoPrecision(v, 6)+'</span>';
    str += '<br>';
	
    d = Math.floor(v);
    v = (v - d) * 60;
    str += sign + d.toString() + '&deg; ' + DoPrecision(v, 3) + "'";
    str += '<br>';
    
    m = Math.floor(v);
    v = (v - m) * 60;
    str += sign + d.toString() + '&deg; ' + m.toString() + "' " + 
        DoPrecision(v, 2) + '"';
	
    id = document.getElementById(out);
    id.innerHTML = str;
}
